/**
 * 计算百分比
 * 保留两位小数，最后为0的都去掉
 */
export function percent(part, total) {
	if (parseInt(total) === 0) {
		return '0%'
	}
	let temp = ((parseInt(part) / parseFloat(total)) * 100).toFixed(2).toString();
	const last = temp.substring(temp.length - 1)
	if (last == 0) {
		temp = temp.substring(0, temp.length - 1)
	}
	const last2 = temp.substring(temp.length - 1)
	if (last2 == 0) {
		temp = temp.substring(0, temp.length - 2)
	}
	return temp + '%'
}

export function percentNumber(part, total) {
	if (parseInt(total) === 0) {
		return 0
	}
	let temp = ((parseInt(part) / parseFloat(total)) * 100).toFixed(2).toString();
	const last = temp.substring(temp.length - 1)
	if (last == 0) {
		temp = temp.substring(0, temp.length - 1)
	}
	const last2 = temp.substring(temp.length - 1)
	if (last2 == 0) {
		temp = temp.substring(0, temp.length - 2)
	}
	return parseFloat(temp)
}

/**
 *  计算百分比，保留两位小数点
 * @param {*} part
 * @param {*} total
 */

export function computePercent(part, total) {
	let res = Math.round(part / total * 10000)
	if (res > 10000) {
		res = 10000
	}
	return res / 100;
}

/**
 * 金额验证,只允许2位小数
 */
export function validateAmount(value) {
	//金额，只允许保留两位小数
	return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
}

/**
 * 生成随即主键
 */
export function randomId(min, max) {
	const rand = Math.floor(min + Math.random() * (max - min))
	return new Date().getTime() + rand
}